﻿/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A
 * copy of the license can be found at http://go.microsoft.com/fwlink/?LinkID=131993. If
 * you cannot locate the Microsoft Public License, please send an email to
 * mixon@microsoft.com. By using this source code in any fashion, you are agreeing to
 * be bound by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

using System.Windows;
using System.Windows.Controls;

namespace Microsoft.Controls
{
    public abstract class VirtualizingPanel : Panel, IVirtualizer
    {
        public IGenerateData DataGenerator { get; set; }
        // This will keep the focused element around if focus is not set to a visible item.
        public UIElement FocusedContainer { get; protected set; }

        public VirtualizingPanel()
        {
            this.Loaded += delegate(object sender, RoutedEventArgs e)
            {
                if (this.DataGenerator == null)
                {
                    //throw new InvalidOperationException("DataGenerator must be set before the panel is loaded.");
                    return;
                }
                this.FocusedContainer = this.DataGenerator.GetContainerForItemOverride();
                var focusedFrameworkElement = this.FocusedContainer as FrameworkElement;
                if (focusedFrameworkElement != null)
                {
                    focusedFrameworkElement.Height = 0;
                }
                this.Children.Add(focusedFrameworkElement); // Make sure it's in the tree (otherwise it can't receive focus).
            };
        }

    }
}
